package com.keyroy.game.puzzle_rpg.models.interfaces;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.wb.swt.ResourceManageX;

public abstract class SWTDrawable implements Drawable {
	private static GC gc;

	public SWTDrawable() {
	};

	@Override
	public void onDraw(Object object) {
		gc = (GC) object;
		onDraw();
	}

	@Override
	public void translate(int tx, int ty) {
		Transform transform = new Transform(null);
		gc.getTransform(transform);
		transform.translate(tx, ty);
		gc.setTransform(transform);
	}

	@Override
	public void drawImage(Object imageObject, int px, int py) {
		gc.drawImage((Image) imageObject, px, py);
	}

	@Override
	public void drawString(String text, int px, int py) {
		gc.drawString(text, px, py, true);
	}

	@Override
	public void drawRect(int px, int py, int pw, int ph) {
		gc.drawRectangle(px, py, pw, ph);
	}

	@Override
	public void fillRect(int px, int py, int pw, int ph) {
		gc.fillRectangle(px, py, pw, ph);
	}

	public void drawArc(int px, int py, int pw, int ph, int sAngle, int eEngle) {
		gc.drawArc(px, py, pw, ph, sAngle, eEngle);
	}

	public void fillArc(int px, int py, int pw, int ph, int sAngle, int eEngle) {
		gc.fillArc(px, py, pw, ph, sAngle, eEngle);
	}

	@Override
	public void setColor(Object color) {
		gc.setBackground((Color) color);
		gc.setForeground((Color) color);
	}

	@Override
	public void setAlpha(int alpha) {
		gc.setAlpha(alpha);
	}

	@Override
	public Object getImage(String path, int width, int height) {
		return ResourceManageX.createImage(path, width, height);
	}

	public Object getColorImage(int color, int width, int height) {
		return ResourceManageX.getImage(color, width, height);
	}

	@Override
	public Object getImage(String path) {
		return ResourceManageX.getImage(path);
	}

	public Object getImage(Class<?> clazz, String path) {
		return ResourceManageX.getImage(clazz, path);
	}

	@Override
	public Object getImage(String path, int dw, int dh, boolean reNULL) {
		return ResourceManageX.loadImage(path, dw, dh, reNULL);
	}

	@Override
	public int imageHeight(Object image) {
		return ((Image) image).getBounds().height;
	}

	@Override
	public int imageWidth(Object image) {
		return ((Image) image).getBounds().width;
	}

	@Override
	public Object getColor(int color) {
		return new Color(null, (color & 0xff0000) >> 16, (color & 0x00ff00) >> 8, color & 0x0000ff);
	}

	@Override
	public int fontHeight() {
		return gc.getFontMetrics().getHeight();
	}

	protected abstract void onDraw();

}
